int main()
{
	srand(time(NULL));
	unsigned char c;
	FILE *f;
	f = fopen("scr.txt","r+");
	int t, x, y, h;
	ran d[100];
	int dif=200;
	//Diem
	int Diem, Score[5] ;
	char Ten[10],Name[5][10];
	for (int i = 0; i < 5; i++)
	{
		fscanf(f, "%s", Name[i]);
		fscanf(f, "%d", &Score[i]);
	}
	while (1) {
	if (Menu(Score, Name,dif)==0) return 0;
	//Bat Dau
	system("cls");	
	gotoxy(65, 2); printf("Score:");
	Diem=0; t=7; h=3; c=1;
	khung();
	do {
	random(x,y);
	gotoxy(x, y); printf("%c", 259);//Moi
	} while (y==11 && x<=t+3);
	for (int i = 0; i < t; i++)
	{
		d[i].x = t - i+2;
		d[i].y = 11;
		gotoxy(d[i].x, d[i].y);
		printf("*");
	}//Khoi tao
	do
	{
		gotoxy(72, 2); printf("%d", Diem);
		Sleep(dif);
		if (_kbhit())
		{
			c = _getch();
			if (c == 224)
			{
				c = _getch();
				if (c == 72 && h != 0) h = 1;
				else if (c == 75 && h != 3)	h = 2;
				else if (c == 77 && h != 2) h = 3;
				else if (c == 80 && h != 1) h = 0;
			}
		}
		gotoxy(d[t - 1].x, d[t - 1].y);
		printf(" ");
		if (d[0].x == x && d[0].y == y)
		{
			for (int i = 0; i < t; i++)
			{
				if (d[i].x == x && d[i].y == y)
				{
					random(x, y);
					i = -1;
				}
			}
			gotoxy(x, y);
			printf("%c", 259);
			t++;
			Diem += 5;
		}
		for (int i = t - 2; i >= 0; i--)
		{
			d[i + 1].x = d[i].x;
			d[i + 1].y = d[i].y;
		}
		if (h == 1) d[0].y -= 1;
		else if (h == 2) d[0].x -= 1;
		else if (h == 3) d[0].x += 1;
		else if (h == 0) d[0].y += 1;
		gotoxy(d[0].x, d[0].y);
		printf("*");
		if (thua(d, t)) c = 0;
	} while ((c != 27) && (c != 0));
	system("cls");
	if (c == 0) printf("\n\n\t\t\t\tGAME OVER!!!");
	Sleep(1000);
	if (Diem > Score[4])
	{
		system("cls");
		printf("\n\n\t\t\t\tDiem cao!");
		Sleep(700);
		system("cls");
		printf("\n\n\t\t\tNhap ten cua ban: "); gets(Ten);
		for (int i = 0; i < 5; i++)
			if (Diem>Score[i])
			{
				for (int j = 3; j >= i; j--)
				{
					Score[j + 1] = Score[j];
					strcpy(Name[i + 1], Name[i]);
				}
				Score[i] = Diem;
				strcpy(Name[i], Ten);
				break;
			}
		f = fopen("scr.txt", "w+");
		for (int i = 0; i < 5; i++)
			fprintf(f, "%s %d\n", Name[i], Score[i]);
	}
	fclose(f);
	hScore(Score,Name);}
}